//
//  GNUserAccount.swift
//  SinaWeChat
//
//  Created by mac on 15/12/7.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit

class GNUserAccount: NSObject,NSCoding {
    
    //用户的id
    var uid:String?
    //过期时间 ,单位秒,注意并不是根据说明来写类型,而是根据具体的格式来写
    var expires_in:NSTimeInterval = 0{
        didSet{
            
            //在外界 expires_in 赋值的时候,就可以去计算出来具体什么时候过期
            expiresDate = NSDate(timeIntervalSinceNow: expires_in)
            print(expiresDate)
            
        }
    }
    // 代表当前帐号是什么时候过期 accessToken 获取回来的那一ke,加上过期的秒数
    var expiresDate:NSDate?
    
    //用于调用access_token,接口获取授权后的access token
    var access_token:String?
    
    //用户的头像地址
    var avatar_large:String?
    
    //用户的昵称
    var screen_name:String?
    
    init(dict:[String:AnyObject]) {
        
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    override var description:String{
        
        
        let keys = ["uid", "expires_in", "access_token","avatar_large", "screen_name"]
        return dictionaryWithValuesForKeys(keys).description
    }
    //MARK:-归档的协议办法
    
    /// 从文件里面读取传成对象,解档
    
    required init(coder aDecoder: NSCoder) {
        
        // 像这种情况下, 前面如果是 ? 类型,就使用 as?
        uid = aDecoder.decodeObjectForKey("uid") as? String
        
        expiresDate  = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        
    }
    
    
    /// 把对象保存成二进制文件,归档
    
    func encodeWithCoder(aCoder: NSCoder) {
        
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        
        
        
    }
    
  

}
